home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / vcolor.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  187 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    vcolor -
  19.  *        Provide virtual rgb and grey scale support.
  20.  *
  21.  *                Paul Haeberli - 1985
  22.  */
  23. #include "gl.h"
  24. #include "get.h"
  25. #define NOGREYPATTERNS
  26. #include "colortbl.inc"
  27. #include "vect.h"
  28.  
  29. long rgb();
  30. long rgbpack();
  31. long rgb3f();
  32. long rgbi();
  33. long grey();
  34. long greyi();
  35. long hvs();
  36. long hvsi();
  37.  
  38. #define MAPNEWS        0
  39. #define MAP332        1
  40. #define REALRGB        2
  41.  
  42. #define MKRGB(r,g,b) ((((r)&0xff)<<0) | (((g)&0xff)<<8) | (((b)&0xff)<<16))
  43.  
  44. static int colormode = MAPNEWS;
  45. static int colorinited;
  46.  
  47. long rgb(fr,fg,fb)
  48. float fr,fg,fb;
  49. {
  50.     int r, g, b;
  51.  
  52.     r = fr*255;
  53.     g = fg*255;
  54.     b = fb*255;
  55.     return rgbi(r,g,b);
  56. }
  57.  
  58. long rgbpack(c)
  59. unsigned long c;
  60. {
  61.     int r, g, b;
  62.  
  63.     r = (c>>0)&0xff;
  64.     g = (c>>8)&0xff;
  65.     b = (c>>16)&0xff;
  66.     return rgbi(r,g,b);
  67. }
  68.  
  69. long rgb3f(c)
  70. float c[3];
  71. {
  72.     return rgb(c[0],c[1],c[2]);
  73. }
  74.  
  75. cpacktovect(c,v)
  76. unsigned long c;
  77. vect *v;
  78. {
  79.     v->x = ((c>>0)&0xff)/255.0;
  80.     v->y = ((c>>8)&0xff)/255.0;
  81.     v->z = ((c>>16)&0xff)/255.0;
  82.     v->w = ((c>>24)&0xff)/255.0;
  83. }
  84.  
  85. long rgbi(r,g,b)
  86. int r,g,b;
  87. {
  88.     long c;
  89.  
  90.     if(!colorinited) 
  91.     colorinit();
  92.     switch(colormode) {
  93.     case MAPNEWS:
  94.         if((r == g) && (g == b)) 
  95.             return greyi(r);
  96.         c = red_inverse[r]+green_inverse[g]+blue_inverse[b];
  97.         color(c);
  98.         break;
  99.     case REALRGB:
  100.         c = MKRGB(r,g,b);
  101.         cpack(c);
  102.     }
  103.     return c;
  104. }
  105.  
  106. long hsv(h,s,v)
  107. float h,s,v;
  108. {
  109.     float r, g, b;
  110.  
  111.     hsv_to_rgb(h,s,v,&r,&g,&b);
  112.     return rgb(r,g,b);
  113. }
  114.  
  115. long hsvi(h,s,v)
  116. int h,s,v;
  117. {
  118.     float fh,fs,fv;
  119.  
  120.     fh = h/255.0;
  121.     fs = s/255.0;
  122.     fv = v/255.0;
  123.     return hsv(fh,fs,fv);
  124. }
  125.  
  126. long grey(fg)
  127. float fg;
  128. {
  129.     return greyi((int)(255*fg));
  130. }
  131.  
  132. long greyi(g)
  133. int g;
  134. {
  135.     long c;
  136.  
  137.     if(!colorinited) 
  138.     colorinit();
  139.     switch(colormode) {
  140.     case MAPNEWS:
  141.         c = grey_inverse[g];
  142.         color(c);
  143.         break;
  144.     case REALRGB:
  145.         c = MKRGB(g,g,g);
  146.         cpack(c);
  147.         break;
  148.     }
  149.     return c;
  150. }
  151.  
  152. usecolor(c)
  153. long c;
  154. {
  155.     switch(colormode) {
  156.     case MAPNEWS:
  157.         color(c);
  158.         break;
  159.     case REALRGB:
  160.         cpack(c);
  161.         break;
  162.     }
  163. }
  164.  
  165. pseudorgb()
  166. {
  167.     if(getgdesc(GD_BITS_NORM_SNG_RED)>0) {
  168.     RGBmode();
  169.     gconfig();
  170.     colormode = REALRGB;
  171.     } else 
  172.     colormode = MAPNEWS;
  173.     colorinited = 1;
  174. }
  175.  
  176. colorinit()
  177. {
  178.     int mode;
  179.  
  180.     mode = getdisplaymode();
  181.     if(mode == DMRGB || mode == DMRGBDOUBLE) 
  182.     colormode = REALRGB;
  183.     else
  184.     colormode = MAPNEWS;
  185.     colorinited = 1;
  186. }
  187.